<?php
namespace api\modules\v1\models;

use api\components\ApiModel;
use api\components\TransmitException;
use Yii;
use api\components\RuleUtil;

/**
 * 信息模型
 * @author WJ
 */
class Basic extends ApiModel
{
    const SCENARIO_PROVINCE_LIST = 'province_list';
    const SCENARIO_CITY_LIST = 'city_list';

    public $country;
    public $code;

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'country' => '国家',
            'code'    => '城市代码',
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return [
            self::SCENARIO_PROVINCE_LIST => ['country'],
            self::SCENARIO_CITY_LIST     => ['code'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['country', 'code'], 'trim'],
            [['country', 'code'], 'required'],
        ];
    }

    /**
     * 获取国家列表
     */
    public function getCountry()
    {
        $url = Yii::$app->params['basic_info']['url'] . Yii::$app->params['basic_info']['method']['countryList'];
        $result = self::curl($url, [], [], 'get');
        if (isset($result['code']) && $result['code'] === 200) {
            return $result['data'];
        } else {
            throw new TransmitException($result['message']);
        }
    }

    /**
     * 获取省份列表
     */
    public function getProvince()
    {
        if (!$this->validate()) {
            self::getErrorMessge($this);
        }
        list($flag, $data) = RuleUtil::checkCountry(strtoupper($this->country));
        if(!$flag){
            self::throwError('', $data);
        }
        if (strtoupper($this->country) == Yii::$app->params['China_code']) {
            $url = Yii::$app->params['basic_info']['url'] . Yii::$app->params['basic_info']['method']['provinceList'];
            $result = self::curl($url, [], [], 'get');
            if (isset($result['code']) && $result['code'] === 200) {
                return $result['data'];
            } else {
                throw new TransmitException($result['message']);
            }
        }else{
            return [];
        }
    }

    /**
     * 获取城市、区域列表
     */
    public function getCity()
    {
        if (!$this->validate()) {
            self::getErrorMessge($this);
        }
        $url = Yii::$app->params['basic_info']['url'] . Yii::$app->params['basic_info']['method']['cityList'] . $this->code;
        $result = self::curl($url, [], [], 'get');
        if (isset($result['code']) && $result['code'] === 200) {
            return $result['data']['data'];
        } else {
            throw new TransmitException($result['message']);
        }
    }

    /**
     * 获取银行列表
     */
    public function getBank()
    {
        $url = Yii::$app->params['basic_info']['url'] . Yii::$app->params['basic_info']['method']['bankList'];
        $result = self::curl($url, [], [], 'get');
        if (isset($result['code']) && $result['code'] === 200) {
            return $result['data'];
        } else {
            throw new TransmitException($result['message']);
        }
    }
}
